import Vue from 'vue'

Vue.directive('focus', {
    inserted(el, obj, vnode) {
        if (!vnode.context.autoFocus) return
        setTimeout(() => {
            var ff = el.querySelector('input')
            if (ff !== null) {
                ff.focus()
            }
        }, 100)
    },
    componentUpdated(el, obj, vnode) {
        if (!vnode.context.autoFocus) return
        const invalidFields = document.querySelector('.invalid-field') || document.querySelector('.van-field--error')
        if ((invalidFields !== null && invalidFields.innerHTML !== '')) {
            return
        }

        const focusField = document.querySelector(':focus')
        if (focusField !== null) {
            return
        }

        var ff = el.querySelector('input')
        if (ff !== null) {
            ff.focus()
        }
    }
})
